require(['date','validator','highcharts','highcharts-more'], function () {
	var beginTime,endTime,carId,$formData=$("#formData");
	
	commonFun.leftTreeNodeClickObj['reportDrivingRecordAnaly'] = function (event, treeId, treeNode) {
		//清空历史记录
		$("span").html("");
		$("#recordAnalysisChart").empty();
		$('#recordAnalysisBtn').click();
	};
	
	$('#recordAnalysisBtn').on('click', function () {
		var result = $.validator($formData);
    	if(result){
			beginTime = $("#carAnaRecordBeginTime").val();
			endTime = $("#carAnaRecordBeginTime").val();
			if(commonFun.getLeftTreeSelectedNodes()!=""){
				carId=commonFun.getLeftTreeSelectedNodes()[0].id;
				loadRecordAnalysisData(carId,beginTime,endTime);
			}else{
				$.messager.alert("提示","请在左侧树中选择车辆，再点击查询。","info");
			}
    	}
	});
	
	function loadRecordAnalysisData(carId,beginTime,endTime){
		$.ajaxExt({
			url:"/carAna/getRecordAnalysisList.htm",
			data : {'carId' : carId,'beginTime' : beginTime,'endTime' : endTime}
		}).done(function(data){
			if(data){
				$(data).each(function(i,item) {
					$("#totRngMile").html(item.totRngMile+"KM");
					$("#totRngEnerCons").html(item.totRngEnerCons+"L");
					$("#totRngOprtDura").html(item.totRngOprtDura);
					$("#ecu1count").html(item.ecu1count+"次");
					$("#ecu2count").html(item.ecu2count+"次");
					$("#ecu3count").html(item.ecu3count+"次");
					$("#bhv99count").html(item.bhv99count+"次");
					$("#bhv11count").html(item.bhv11count+"次");
					$("#bhv12count").html(item.bhv12count+"次");
					$("#bhv5count").html(item.bhv5count+"次");
				});
			}else{
				$.messager.alert("提示","该日期暂无数据","info");
			}
		}); 
		$.ajaxExt({
			url : "/carAna/getRecordAnalysisChart.htm",
			data : {'carId' : carId,'beginTime' : beginTime,'endTime' : endTime}
		}).done(function(result){
			var spdOfMotVhiArr=[] ;
			var engiRtteSpdArr = [];
			var instFuelCsutArr = [];
			var acltPedalOpeArr = [];
			var electThroOpeArr = [];
			var oxygenConcenArr = [];
			var engiAirAdmissionTeptArr = [];
			var airAdmissionPrsArr = [];
			$(result).each(function(i,item) {
				spdOfMotVhiArr.push([item.recordTime ,item.spdOfMotVhi||null]);
				engiRtteSpdArr.push([item.recordTime , item.engiRtteSpd||null]);
				acltPedalOpeArr.push([item.recordTime , item.acltPedalOpe||null]);
				electThroOpeArr.push([item.recordTime , item.electThroOpe||null]);
				oxygenConcenArr.push([item.recordTime , item.oxygenConcen||null]);
				engiAirAdmissionTeptArr.push([item.recordTime , item.engiAirAdmissionTept||null]);
				airAdmissionPrsArr.push([item.recordTime , item.engiRtteSpd||null]);
				instFuelCsutArr.push([item.recordTime , item.instFuelCsut||null]);
			});
			createRecordChart(spdOfMotVhiArr, engiRtteSpdArr, acltPedalOpeArr, electThroOpeArr, oxygenConcenArr,engiAirAdmissionTeptArr,airAdmissionPrsArr,instFuelCsutArr);
		});
	}
			
	function createRecordChart(spdOfMotVhiArr, engiRtteSpdArr, acltPedalOpeArr, electThroOpeArr, oxygenConcenArr,engiAirAdmissionTeptArr,airAdmissionPrsArr,instFuelCsutArr){
		$('#recordAnalysisChart').highcharts({
			chart: {
				zoomType: 'x',
				plotBorderWidth: 1,
				marginTop: 38,
				marginLeft: 90,
				marginRight: 180,
				height:$(document).height()-180,
				width:$(document).width()-50
			},
			title: {
				text: ''
			},
			credits: {
				enabled: false
			},
			tooltip: {
				shared: true,
				formatter: function () {
					var html = '时间:' + new Date(this.x-1000*60*60*8).format('yyyy-MM-dd hh:mm:ss') + '<br>';
					$.each(this.points, function (index, item) {
						if (item.series.name == '瞬时油耗') {
							html += '瞬时油耗:' + item.y + ' L/H<br>';
						} 
						if (item.series.name == '油门踏板开度') {
							html += '油门踏板开度:' + item.y + ' %<br>';
						}
						if (item.series.name == '电子节气门开度') {
							html += '电子节气门开度:' + item.y + ' %<br>';
						}
						if (item.series.name == '氧浓度') {
							html += '氧浓度:' + item.y + ' %<br>';
						}
						if (item.series.name == '进气温度') {
							html += '进气温度:' + item.y + ' ℃<br>';
						}
						if (item.series.name == '进气压力') {
							html += '进气压力:' + item.y + ' mpa<br>';
						}
						if (item.series.name == '车速') {
							html += '车速:' + item.y + ' Km/h<br>';
						}
						if (item.series.name == '转速') {
							html += '转速:' + item.y + ' r/min<br>';
						}
					});
					return html;
				},
				crosshairs: true
			},
			xAxis: {
				tickmarkPlacement: 'on',
				type: 'datetime'
			},
			yAxis: [
		    	{
					min: 0,
					title: {
						text: 'L/H',
						offset: 30,
						rotation: 0,
						y: -10,
						align: 'high'
					},
					opposite: true,
					max: 50
				},
				{
					min: -100,
					title: {
						text: '%',
						offset: 30,
						rotation: 0,
						y: -10,
						align: 'high'
					},
					opposite: true,
					max: 100
				},
				{
					min: 0,
					title: {
						text: '℃',
						offset: 30,
						rotation: 0,
						y: -10,
						align: 'high'
					},
					opposite: true,
					max: 100
				},
				{
					min: 0,
					title: {
						text: 'mpa',
						offset: 30,
						rotation: 0,
						y: -10,
						align: 'high'
					},
					opposite: true,
					max: 2000
				},
				{
					min: 0,
					title: {
						text: 'Km/h',
						offset: 0,
						rotation: 0,
						y: -10,
						align: 'high'
					},
					max: 120
				},
				{ 
					min: 0,
					title: {
						text: 'r/min',
						offset: 10,
						rotation: 0,
						y: -10,
						align: 'high'
					}
				}
			],
			series: [
				{
					data: instFuelCsutArr,
					name: '瞬时油耗',
					marker: {
						enabled: false
					}
				},
				{
					data: acltPedalOpeArr,
					name: '油门踏板开度',
					yAxis: 1,
					marker: {
						enabled: false
					}
				},
				{
					data: electThroOpeArr,
					name: '电子节气门开度',
					yAxis: 1,
					marker: {
						enabled: false
					}
				},
				{
					data: oxygenConcenArr,
					name: '氧浓度',
					yAxis: 1,
					marker: {
						enabled: false
					}
				},
				{
					data: engiAirAdmissionTeptArr,
					name: '进气温度',
					yAxis: 2,
					marker: {
						enabled: false
					}
				},
				{
					data: airAdmissionPrsArr,
					name: '进气压力',
					yAxis: 3,
					marker: {
						enabled: false
					}
				},
				{
					data: spdOfMotVhiArr,
					name: '车速',
					yAxis: 4,
					marker: {
						enabled: false
					}
				},
				{
					data: engiRtteSpdArr,
					name: '转速',
					yAxis: 5,
					marker: {
						enabled: false
					}
				}
			]
		});
	}
});